package short_url_type

import (
	"gitee.com/xlizy/common-go/base/enums"
	"strconv"
)

type ShortUrlType int

const (
	Perpetual ShortUrlType = iota + 1
	Times
	TimeLimit
	TimeLimitAndTimes
)

func (e ShortUrlType) Code() int32 {
	return int32(e)
}

func (e ShortUrlType) Des() string {
	return enums.BE(e).Des
}

func (e ShortUrlType) Enum() string {
	return enums.BE(e).Enum
}

func (e ShortUrlType) Val() int {
	return int(e)
}

func (e *ShortUrlType) UnmarshalJSON(value []byte) error {
	*e = ShortUrlType(enums.UnmarshalEnum(value))
	return nil
}

func (e ShortUrlType) MarshalJSON() ([]byte, error) {
	switch e {
	case Perpetual:
		return enums.JsonObj(int(e), "Perpetual", "永久")
	case Times:
		return enums.JsonObj(int(e), "Times", "次数限制")
	case TimeLimit:
		return enums.JsonObj(int(e), "TimeLimit", "限时")
	case TimeLimitAndTimes:
		return enums.JsonObj(int(e), "TimeLimitAndTimes", "限时次数")
	}
	return []byte(strconv.Itoa(int(e))), nil
}

func SelectOptions() []enums.SelectOptions {
	return enums.ToSelectOptions([]any{Perpetual, Times, TimeLimit, TimeLimitAndTimes})
}
